//---------------------------------------------------------------------------
// MyResultSet Class
// Author : Alex Woo-Hyun Kim at www.webrobot.org
// Email : woorung@webrobot.org,webrobot@hotmail.com,woorung@empal.com
// Date : Apr. 18, 2002
//
// This class manages the result set obtained by CMyMan class. 
//---------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <mysql.h>
#include "CMyResultSet.h"
//---------------------------------------------------------------------------
CMyResultSet::CMyResultSet(unsigned long ulAffectedRows)
{
	m_uMyCurColumnCount = 0;
	m_ulMyCurRowCount = 0;
	m_uMyMetaNameCount = 0;
	m_pMyResultSet = NULL;
	m_pMyRow = NULL;
	m_pMyFields = NULL;
	m_pMyResultSet = NULL;
	m_uMyColumnCount = 0;
	m_ulMyRowCount = ulAffectedRows;	
}
//---------------------------------------------------------------------------
CMyResultSet::CMyResultSet(MYSQL_RES *pMyResultSet)
{
	m_uMyCurColumnCount = 0;
	m_ulMyCurRowCount = 0;
	m_uMyMetaNameCount = 0;
	m_pMyResultSet = NULL;
	m_pMyRow = NULL;
	m_pMyFields = NULL;
	m_pMyResultSet = pMyResultSet;
	m_uMyColumnCount = mysql_num_fields(pMyResultSet);
#ifdef DEBUG
	Debug("Column count is [");
	Debug((int) m_uMyColumnCount);
	Debug("]\n");
#endif
	m_ulMyRowCount = mysql_num_rows(pMyResultSet);
#ifdef DEBUG
	Debug("Total row count is [");
	Debug((int) m_ulMyRowCount);
	Debug("]\n");
#endif
}
//---------------------------------------------------------------------------
CMyResultSet::~CMyResultSet()
{
	if(m_pMyResultSet!=NULL) {
		mysql_free_result(m_pMyResultSet);
		m_pMyResultSet = NULL;
	}
}
//---------------------------------------------------------------------------
bool CMyResultSet::HasMoreRowsFromMyResultSet()
{
	if(m_pMyResultSet==NULL) return false;
	if((m_pMyRow=mysql_fetch_row(m_pMyResultSet))!=NULL) {
		m_ulMyCurRowCount++;
		m_uMyCurColumnCount = 0;  // attention !!
#ifdef DEBUG
		static int i=0;

		Debug("\nfetch row[");
		Debug(i++);
		Debugln("] okay");
#endif
		return true;
	}
	else return false;
}
//---------------------------------------------------------------------------
bool CMyResultSet::HasMoreColumnFromCurRows()
{
	if(m_pMyRow==NULL) HasMoreRowsFromMyResultSet();

	if(m_uMyCurColumnCount++<m_uMyColumnCount) return true;
	else return false;
}
//---------------------------------------------------------------------------
bool CMyResultSet::HasMoreMetaNameFromMyResultSet()
{
	if(m_uMyMetaNameCount++<m_uMyColumnCount) {
#ifdef DEBUG
		static int i=0;
		
		Debug("meta data[");
		Debug(i++);
		Debugln("] okay");
#endif
		return true;
	}
	else return false;		
}
//---------------------------------------------------------------------------
unsigned long CMyResultSet::GetAffectedRows()
{
	if(m_pMyResultSet==NULL) return m_ulMyRowCount;
	else return m_ulMyRowCount;
}
//---------------------------------------------------------------------------
const char *CMyResultSet::GetString()
{
	if(m_uMyCurColumnCount==0) HasMoreColumnFromCurRows();

#ifdef DEBUG
	Debug("This is column[");
	Debug(m_uMyCurColumnCount-1);
	Debug("] : ");
	Debugln((char *) (m_pMyRow[m_uMyCurColumnCount-1]!=NULL?m_pMyRow[m_uMyCurColumnCount-1]:"NULL"));
#endif

	return m_pMyRow[m_uMyCurColumnCount-1]!=NULL?m_pMyRow[m_uMyCurColumnCount-1]:"NULL";
}
//---------------------------------------------------------------------------
const char *CMyResultSet::GetString(int iColumnCount)
{
	if(iColumnCount>=m_uMyColumnCount) return "NULL";
	if(m_pMyRow==NULL) HasMoreRowsFromMyResultSet();

#ifdef DEBUG
	Debug("This is column[");
	Debug(iColumnCount);
	Debug("] : ");
	Debugln((char *) (m_pMyRow[iColumnCount]!=NULL?m_pMyRow[iColumnCount]:"NULL"));
#endif

	return m_pMyRow[iColumnCount]!=NULL?m_pMyRow[iColumnCount]:"NULL";	
}
//---------------------------------------------------------------------------
const char *CMyResultSet::GetString(char *pszColumnName)
{
	int i;

	if(m_pMyFields==NULL) {
		m_pMyFields = mysql_fetch_fields(m_pMyResultSet);	
	}	

	for(i=0;i<m_uMyColumnCount;i++){
		if(!strncmp(pszColumnName,m_pMyFields[i].name,strlen(pszColumnName))) {
			return GetString(i);	
		}
	}

	return "NULL";  // there is no such column name in table 
}
//---------------------------------------------------------------------------
const char *CMyResultSet::GetMetaNameString()
{
	if(m_uMyMetaNameCount==0) HasMoreMetaNameFromMyResultSet();
	if(m_pMyFields==NULL) {
                m_pMyFields = mysql_fetch_fields(m_pMyResultSet);
        }

#ifdef DEBUG
	Debug("\nThis is metafield name of column[");
	Debug(m_uMyMetaNameCount-1);
	Debug("] : ");
	Debugln((char *) m_pMyFields[m_uMyMetaNameCount-1].name);
#endif

	return m_pMyFields[m_uMyMetaNameCount-1].name; 
}
//---------------------------------------------------------------------------
const char *CMyResultSet::GetMetaNameString(int iColumnCount)
{
	if(iColumnCount>=m_uMyColumnCount) return "NULL";
	if(m_pMyFields==NULL) {
		m_pMyFields = mysql_fetch_fields(m_pMyResultSet);
	}

#ifdef DEBUG
	Debug("\nThis is metafield name of column[");
	Debug(iColumnCount);
	Debug("] : ");	
	Debugln((char *) m_pMyFields[iColumnCount].name);
#endif

	return m_pMyFields[iColumnCount].name; 
}
//---------------------------------------------------------------------------
#ifdef DEBUG
void CMyResultSet::Debug(char *str)
{
        FILE *f = fopen("debug","a+");
        fprintf(f,"%s",str);
        fclose(f);
}
void CMyResultSet::Debugln(char *str)
{
        FILE *f = fopen("debug","a+");
        fprintf(f,"%s\n",str);
        fclose(f);
}
void CMyResultSet::Debug(int i)
{
        FILE *f = fopen("debug","a+");
        fprintf(f,"%d",i);
        fclose(f);
}
void CMyResultSet::Debugln(int i)
{
        FILE *f = fopen("debug","a+");
        fprintf(f,"%d\n",i);
        fclose(f);
}
#endif
//--------------------------------------------------------------------------
